package com.gitee.wsl.android.ui.spannable.span

import android.graphics.Typeface
import android.text.TextPaint
import android.text.style.TypefaceSpan

class TypefaceSpanCompat(private val newType: Typeface) : TypefaceSpan(null) {
    override fun updateDrawState(ds: TextPaint) = ds.applyTypeFace(newType)

    override fun updateMeasureState(paint: TextPaint) = paint.applyTypeFace(newType)

    private fun TextPaint.applyTypeFace(tf: Typeface) {
        val oldStyle: Int
        val old = typeface
        oldStyle = old?.style ?: 0
        val fake = oldStyle and tf.style.inv()
        if (fake and Typeface.BOLD != 0) {
            isFakeBoldText = true
        }
        if (fake and Typeface.ITALIC != 0) {
            textSkewX = -0.25f
        }
        typeface = tf
    }
}